<title>菜单表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/SysMenu/GetIndex" lay-done="layui.data.done(d);">
</script>

<script type="text/html" id="LAY-app-SysMenu-tableBox-toolbar">
    <div class="layui-form coreshop-toolbar-search-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <button class="layui-btn  layui-btn-sm mybtn  layuiadmin-btn-admin" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
            </div>
        </div>
    </div>
</script>


<div class="table-body">
    <table id="LAY-app-SysMenu-tableBox" lay-filter="LAY-app-SysMenu-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-SysMenu-tableBox-bar">

    {{# if(d.otherData.path != '' && d.otherData.path != null && d.otherData.menuType===0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="importButton">导入按钮</a>
    {{# } }}

    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" data-dropdown="#SysMenuTbDelDrop{{d.id}}" no-shade="true">删除</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide" id="SysMenuTbDelDrop{{d.id}}"
         style="max-width: 200px; white-space: normal; min-width: auto; margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除【{{d.otherData.menuName}}】吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn layui-btn-primary cursor" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal cursor" lay-event="del">确定</a>
        </div>
    </div>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;

    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coredropdown', 'coreHelper','zTree'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , treeTable = layui.treeTable
                    , util = layui.util
                    , zTree = layui.zTree
                    , view = layui.view;

                //数据绑定
              var  insTb = treeTable.render({
                    id: 'treeTableBox',
                  url: layui.setter.apiUrl + 'Api/SysMenu/GetPageList',
                  where: { type: 'layui' },
                    method: 'POST',
                    elem: '#LAY-app-SysMenu-tableBox',
                    tree: {
                        customName: {
                            id: 'id',
                            pid: 'parentId',
                            name: 'menuName',
                        },
                        view: {
                            showIcon: false,
                            indent:25
                        },
                        data: {
                            isSimpleData: false,
                        }
                    },
                    height: 'full-127',//无面包屑127,搜索框189,1行62
                    toolbar: '#LAY-app-SysMenu-tableBox-toolbar',
                        cols: [[
                            {field: 'id', title: '序列', width: 60},
                            { field: 'menuName', title: '菜单名称', minWidth: 110, templet: function (d) { return d.otherData.menuName } },
                            {
                                title: '图标', templet: '<p><i class="layui-icon {{d.otherData.menuIcon}}"></i></p>',
                                align: 'center', width: 60, minWidth: 60
                            },
                            { field: 'path', title: '菜单Url', templet: function (d) { return d.otherData.path == null || d.otherData.path == '' ? '' : d.otherData.path } },
                            { field: 'identificationCode', title: '文件夹标识', templet: function (d) { return d.otherData.identificationCode } },
                            { field: 'component', title: 'API请求地址', templet: function (d) { return d.otherData.component } },
                            { field: 'authority', title: '权限标识', templet: function (d) { return d.otherData.authority } },
                            {
                                field: 'sortNumber', title: '排序', align: 'center', width: 60, minWidth: 60, templet: function (d) { return d.otherData.sortNumber }
                            },
                            {
                                title: '隐藏', templet: function (d) {
                                    return d.otherData.hide ? '<span class="text-danger">隐藏</span>' : '显示';
                                }, align: 'center', width: 60, minWidth: 60
                            },
                            {
                                title: '类型', templet: function (d) {
                                    var strs = ['<span class="layui-badge layui-badge-green">菜单</span>', '<span class="layui-badge layui-badge-gray">按钮</span>'];
                                    return strs[d.otherData.menuType];
                                }, align: 'center', width: 70, minWidth: 70
                            },
                            //{field: 'createTime', title: '创建时间'},
                            {title: '操作', toolbar: '#LAY-app-SysMenu-tableBox-bar', align: 'center', width: 180}
                    ]],

                      //autoSort: false,
                      page: false
                });
                    //重载form
                form.render();

                     //事件
                var active = {
                    add: function () {
                        doCreate();
                    }
                };
                $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });

                //监听工具条
                treeTable.on('tool(LAY-app-SysMenu-tableBox)',
                    function (obj) {
                        if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }else if (obj.event === 'importButton') {
                            doImportButton(obj)
                        }
                    });
                //执行创建操作
                function doCreate() {
                    coreHelper.Post("Api/SysMenu/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            var treeTableData = treeTable.getData('treeTableBox',false);
                            admin.popup({ shadeClose: false,
                                title: '创建数据',
                                area: ['1000px', '500px'],
                                id: 'LAY-popup-SysMenu-create',
                                success: function (layero, index) {
                                    view(this.id).render('system/sysmenu/create', { data: e.data, treeTableData: treeTableData }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-SysMenu-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                if (!field.parentId) {
                                                    field.parentId = 0;
                                                }

                                                field.hide = field.hide == 1;
                                                //field.deleted = field.deleted == 'on';

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/SysMenu/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        insTb.reloadData();
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                    // 弹窗不出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-SysMenu-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/SysMenu/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            var treeTableData = treeTable.getData('treeTableBox', false);
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['1000px', '500px'],
                                id: 'LAY-popup-SysMenu-edit',
                                success: function (layero, index) {
                                    view(this.id).render('system/sysmenu/edit', { data: e.data, treeTableData: treeTableData }).done(function() {
                                        //监听提交
                                        form.on('submit(LAY-app-SysMenu-editForm-submit)',
                                            function(data) {
                                                var field = data.field; //获取提交的字段
                                                if (!field.parentId) {
                                                    field.parentId = 0;
                                                }
                                                field.hide = field.hide == 1;
                                                //field.deleted = field.deleted == 'on';
                                                if (debug) {
                                                    console.log(field);
                                                } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/SysMenu/DoEdit",
                                                    field,
                                                    function(e) {
                                                        console.log(e)
                                                        if (e.code === 0) {
                                                            insTb.reloadData();
                                                            layer.close(index); //再执行关闭
                                                            layer.msg(e.msg);
                                                        } else {
                                                            layer.msg(e.msg);
                                                        }
                                                    });
                                            });
                                    });
                                    // 禁止弹窗出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-SysMenu-editForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }

                //执行权限设置操作
                function doImportButton(obj) {
                    console.log(obj);
                    admin.popup({ shadeClose: false,
                        title: '按钮选择',
                        area: ['600px', '90%'],
                        btn: ['保存', '取消'],
                        content: '<ul id="roleAuthTree" class="ztree" style="padding-left: 15px;min-height: 90%;"></ul>',
                        success: function (layero, dIndex) {
                            var loadIndex = layer.load(2);
                            coreHelper.Post('Api/Tools/GetAllControllerAndActionByAssembly', null, function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    $.fn.zTree.init($('#roleAuthTree'), {
                                        check: { enable: true },
                                        data: {
                                            key: {
                                                name: "name",
                                                children:"action"
                                            },
                                        }
                                    }, res.data);
                                } else {
                                    layer.msg(res.msg, { icon: 2, anim: 6 });
                                }
                            });
                            // 超出一定高度滚动
                            //$(layero).children('.layui-layer-content').css({ 'max-height': '300px', 'overflow': 'auto' });
                        },
                        yes: function (dIndex) {
                            var checkedRows = $.fn.zTree.getZTreeObj('roleAuthTree').getCheckedNodes(true);
                            var ids = [];
                            var postData = [];
                            for (var i = 0; i < checkedRows.length; i++) {
                                ids.push(checkedRows[i].id);
                                if (checkedRows[i].type=='action') {
                                    let row = {
                                        menuId:obj.data.id,
                                        controllerName:checkedRows[i].controllerName,
                                        actionName:checkedRows[i].actionName,
                                        description:checkedRows[i].description,
                                        type:checkedRows[i].type,
                                    }
                                    postData.push(row);
                                }
                            }
                            console.log(postData);

                            var loadIndex = layer.load(2);
                            coreHelper.Post('Api/SysMenu/ImportButtons', {data: postData }, function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    layer.msg(res.msg, { icon: 1 });
                                    insTb.reloadData();
                                    layer.close(dIndex);
                                } else {
                                    layer.msg(res.msg, { icon: 2, anim: 6 });
                                }
                            });
                        }
                    });
                }

                //执行单个删除
                function doDelete(obj) {
                    console.log(obj);
                    coreHelper.Post("Api/SysMenu/DoDelete", { id: obj.data.id }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        //table.reloadData('LAY-app-SysMenu-tableBox');
                        insTb.reloadData();
                        layer.msg(e.msg);
                    });
                }

            });
    };
</script>